import json
from PySide6.QtCore import QDateTime

from Devices.Chiot.BsDevs.ComDev.ComDevParser import ComDevParser
from Devices.Chiot.BsDevs.Dimmer.DimmerDevice import DimmerDevice

class DimmerParser(ComDevParser):
    def __ParseCom(stateValue, fullStatus):
        ret, value = DimmerParser.GetIntProperty(stateValue, "switch", "on")
        if ret:
            fullStatus.switchStatus = value

        ret, value = DimmerParser.GetIntProperty(stateValue, "brightness", "brightness")
        if ret:
            fullStatus.brightness = value

        ret, value = DimmerParser.GetIntProperty(stateValue, "memSwitch", "on")
        if ret:
            fullStatus.memSwitchStatus = value

        ret, value = DimmerParser.GetIntProperty(stateValue, "backlight", "on")
        if ret:
            fullStatus.backlightStatus = value

    def ParseHeartbeat(msgValue, fullStatus):
        ret, value = DimmerParser.GetIntProperty(msgValue, "voltageFreq")
        if ret:
            fullStatus.voltageFreq = value

        ret, value = DimmerParser.GetIntProperty(msgValue, "pwmDuty")
        if ret:
            fullStatus.pwmDuty = value

        if not "stateValue" in msgValue:
            return True
        stateValue = msgValue["stateValue"]

        DimmerParser.__ParseCom(stateValue, fullStatus)
        
        DimmerParser.GetTimers(stateValue, "timer", fullStatus.timers, DimmerDevice.Timer())
        DimmerParser.GetDelay(stateValue, "delay", fullStatus.delay)
        return True

    def ParseRptStatus(msgValue, fullStatus):
        if not "stateValue" in msgValue:
            return False
        stateValue = msgValue["stateValue"]

        DimmerParser.__ParseCom(stateValue, fullStatus)
        
        return True

    # duty:12,"freq":60
    def PackSetVoltageParam(paramName, paramValue):
        data = {}
        data["client"] = "NetworkTest"
        data["time"] = QDateTime.currentSecsSinceEpoch()
        data["cmd"] = "setVoltageParam"
        data[paramName] = paramValue
        
        return json.dumps(data, separators=(',', ':'))